//
//  Theme.m
//  LYPhotos
//
//  Created by 美鑫科技 on 16/5/5.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "Theme.h"
#import <objc/message.h>

@implementation Theme

- (instancetype)init{
    self = [super init];
    if (self) {
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"theme"]) {
            self.themeColor = RGBCOLOR(35, 103, 255);
            self.themeBodyColor = RGBCOLOR(246, 246, 246);
            self.themeTextColor = RGBCOLOR(255, 255, 255);
            [self saveTheme:self];
        } else{
            self = [self getUITheme];
        }
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    [super setValue:value forUndefinedKey:key];
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int propertyCount = 0;
    objc_property_t *propertys = class_copyPropertyList([self class], &propertyCount);
    for (int i = 0; i < propertyCount; i++) {
        objc_property_t property = propertys[i];
        const char * key = property_getName(property);
        id value = [self valueForKey:[NSString stringWithUTF8String:key]];
        NSString *attr = [NSString stringWithUTF8String:property_getAttributes(property)];
        if ([attr componentsSeparatedByString:@","].count == 3) {
            [aCoder encodeInteger:[value integerValue] forKey:[NSString stringWithUTF8String:key]];
        } else{
            [aCoder encodeObject:value forKey:[NSString stringWithUTF8String:key]];
        }
    }
    free(propertys);
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        unsigned int propertyCount = 0;
        objc_property_t *propertys = class_copyPropertyList([self class], &propertyCount);
        for (int i = 0; i < propertyCount; i++) {
            objc_property_t property = propertys[i];
            const char * key = property_getName(property);
            NSString *attr = [NSString stringWithUTF8String:property_getAttributes(property)];
            if ([attr componentsSeparatedByString:@","].count == 3) {
                [self setValue:@([aDecoder decodeIntegerForKey:[NSString stringWithUTF8String:key]]) forKey:[NSString stringWithUTF8String:key]];
            } else{
                [self setValue:[aDecoder decodeObjectForKey:[NSString stringWithUTF8String:key]] forKey:[NSString stringWithUTF8String:key]];
            }
        }
        free(propertys);
    }
    return self;
}

- (void)saveTheme:(Theme *)theme{
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:theme forKey:@"theme"];
    [archiver finishEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theme"];
}

- (Theme *)getUITheme{
    NSData *deData = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];
    NSKeyedUnarchiver *uKey = [[NSKeyedUnarchiver alloc] initForReadingWithData:deData];
    Theme *theme = [uKey decodeObjectForKey:@"theme"];
    return theme;
}

@end
